package org.example.controller;

import org.example.entity.DeviceType;
import org.example.service.DeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 设备类型控制器
 */
@RestController
@RequestMapping("/device-types")
@CrossOrigin(origins = "*")
public class DeviceTypeController {
    
    @Autowired
    private DeviceTypeService deviceTypeService;
    
    /**
     * 创建设备类型
     */
    @PostMapping
    public ResponseEntity<?> createDeviceType(@Valid @RequestBody DeviceType deviceType) {
        try {
            DeviceType createdDeviceType = deviceTypeService.createDeviceType(deviceType);
            return ResponseEntity.status(HttpStatus.CREATED).body(createdDeviceType);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 根据ID获取设备类型
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getDeviceTypeById(@PathVariable Long id) {
        Optional<DeviceType> deviceType = deviceTypeService.findById(id);
        if (deviceType.isPresent()) {
            return ResponseEntity.ok(deviceType.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 根据名称获取设备类型
     */
    @GetMapping("/name/{name}")
    public ResponseEntity<?> getDeviceTypeByName(@PathVariable String name) {
        Optional<DeviceType> deviceType = deviceTypeService.findByName(name);
        if (deviceType.isPresent()) {
            return ResponseEntity.ok(deviceType.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 获取所有设备类型
     */
    @GetMapping
    public ResponseEntity<List<DeviceType>> getAllDeviceTypes() {
        List<DeviceType> deviceTypes = deviceTypeService.findAllDeviceTypes();
        return ResponseEntity.ok(deviceTypes);
    }
    
    /**
     * 根据名称搜索设备类型
     */
    @GetMapping("/search")
    public ResponseEntity<List<DeviceType>> searchDeviceTypes(@RequestParam String name) {
        List<DeviceType> deviceTypes = deviceTypeService.searchDeviceTypesByName(name);
        return ResponseEntity.ok(deviceTypes);
    }
    
    /**
     * 更新设备类型
     */
    @PutMapping("/{id}")
    public ResponseEntity<?> updateDeviceType(@PathVariable Long id, @Valid @RequestBody DeviceType deviceType) {
        try {
            deviceType.setId(id);
            DeviceType updatedDeviceType = deviceTypeService.updateDeviceType(deviceType);
            return ResponseEntity.ok(updatedDeviceType);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 统计设备类型使用次数
     */
    @GetMapping("/{id}/usage-count")
    public ResponseEntity<Long> getDeviceTypeUsageCount(@PathVariable Long id) {
        long count = deviceTypeService.countDevicesByType(id);
        return ResponseEntity.ok(count);
    }
    
    /**
     * 删除设备类型
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteDeviceType(@PathVariable Long id) {
        try {
            deviceTypeService.deleteDeviceType(id);
            return ResponseEntity.ok("设备类型删除成功");
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}
